﻿<?php
/**
 * 对用户的主题就行管理
 * @author kinet <380511703@qq.com>
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 */

session_start();
require_once( 'path.php');
require_once( SOJ_ROOT.'/include/function/lib.php');

$tpl = new my_smarty();

//若未登录,则重定向到登录界面
if( empty($_SESSION['admin']) ){

	$_SESSION['success_message'] = 'do not login yet , '.$_SESSION['admin'];
	header( 'Location:../manager/manager_login.php' );
	exit ;
}
$request = $_GET;

if( empty( $request['tno'])||empty($request['op'])||empty($request['username']) ){
	//problem,直接跳转到problem页面
	header( 'Location:../manager/manager_index.php');
	exit ;
}else{
	$tno =$request['tno'];
	$op =$request['op'];
	$username=$request['username'];	
}


//处理由其他页面保存在会话中的错误信息
if( !empty($_SESSION['errors']) ){

	$tpl->assign( 'errors' , $_SESSION['errors']  );
	//清除之前的错误信息
	unset( $_SESSION['errors'] );
}
$tpl->assign('admin', $_SESSION['admin']);
$tpl->assign('username', $username);


try{
	  $db = db_connect();
	  
	  if($op==="look"){
	  
	    $topic_process = new topic_process( $db );
	    $topic_process->load( 'topic_no' , $tno ) ;  
	    $title = $topic_process->get( 'title' );
	    $content = $topic_process->get( 'content' );
	    $post_time = $topic_process->get( 'post_time' );
	    $view_count = $topic_process->get( 'view_count' );
	    $reply_count = $topic_process->get( 'reply_count' );
	    $username = $topic_process->get( 'username' );
		$status=$topic_process->get('status');
		$tpl->assign( 'topic_no' , $tno ); 		
		$tpl->assign( 'title' , $title );
		$tpl->assign( 'content' , $content );
		$tpl->assign( 'post_time' , $post_time );
		$tpl->assign( 'view_count' , $view_count );
		$tpl->assign( 'reply_count' , $reply_count );
		$tpl->assign('status',$status);
		$tpl->assign( 'post_username' , $username );

		//是否有回复
		if( $reply_count > 0 ){
			
			$replys = $topic_process->get_all_reply();
			$tpl->assign( 'replys' , $replys );
		}
		$tpl->display('manager_topic_detail.tpl');
	  }
	  else if($op==="close"){
	     $topic_process = new topic_process( $db );
	     $topic_process->load( 'topic_no' , $tno ) ;  
	     $topic_process->set_status('close');	 
         $topic_process->save();	
         if($username!="null")		 
		 header( 'Location:../manager/manager_user_topic.php?name='.$username);
		 else{
		 header( 'Location:../manager/manager_topic.php');
		 }
	   
	  }	  
	  else if($op==="open"){
	     $topic_process = new topic_process( $db );
	     $topic_process->load( 'topic_no' , $tno ) ;  
	     $topic_process->set_status('open');
         $topic_process->save();	
         if($username!="null")			 
		 header( 'Location:../manager/manager_user_topic.php?name='.$username);			
		 else{
		 header( 'Location:../manager/manager_topic.php');
		 }
		  
	  }
	  else if($op==="delete"){
	  
			$topic_process = new topic_process( $db );
			$topic_process->load( 'topic_no' , $tno ) ;  
		    $topic_process->delete();
			$reply_process=new reply_process($db);
			$reply_process->delete_by_topic_no($tno);
			if($username!="null")		
		    header( 'Location:../manager/manager_user_topic.php?name='.$username);
			 else{
		     header( 'Location:../manager/manager_topic.php');
		   }
		  
	  }	  
}catch(Exception $e){
	  echo $e->getMessage();
}
